// pages/classify/classify.js
import * as API from "../../utils/config.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth:0, 
    winHeight:0, 
    currentTab:0,
    classifyList:[],
    list:{},
    item:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this; 
    console.log(options)
  
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({ 
    
     success: function (res) { 
      that.setData({ 
       winWidth: res.windowWidth, 
       winHeight: res.windowHeight 
      }); 
     } 
    
    });
    if(!wx.getStorageSync("classify")){
      this._getData();
    }else{
      wx.getStorage({
        key: 'classify',
        success: function(res){
          // success
          that.setData({
            "list":res.data
          })
        },
      })
    }
    // else{
    //   wx.getStorage({
    //     key: 'classify',
    //     success: (res)=>{
    //       // success
    //       // console.log(res)
    //       this.setData({
    //         "classify":res.data
    //       })
    //     }
    //   })
    // }
    
    // this._setData();
  },
  onReady:function(){
    
  },
  bindChange: function (e) { 
  
    var that = this; 
    that.setData({ currentTab: e.detail.current }); 
    
   }, 
   swichNav: function (e) { 
    
    var that = this; 
    
    if (this.data.currentTab === e.target.dataset.current) { 
     return false; 
    } else { 
     that.setData({ 
      currentTab: e.target.dataset.current 
     }) 
    } 
   } ,
   bgg(event){
     console.log(event)
    wx.navigateTo({
      url: "../content/content?name="+event.currentTarget.dataset.name,
    })
  },
  _getData:function(){
    let self = this;
    let data = {};
    let data1 = {};
    wx.request({
      url: API.classify,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log(res.data.result)
        let categoryList = res.data.result
        categoryList.forEach((value,index) => {
          wx.request({
            url: "https://easy-mock.com/mock/5ae18553ebc6c061022b9a7f/comic/book?type="+value,
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function(res){
              // success
              data[index] = res.data.data.result.bookList;
              self.data.list[index] = res.data.data.result.bookList;
              wx.setStorage({
                key:"classify",
                data:self.data.list
              })
              self.setData({
                list:self.data.list,
               
              })
            },
          })
              
        });
      },
    })
    // console.log(list)
    // console.log(data1)
  }, 
   /** 
    * 页面相关事件处理函数--监听用户下拉动作 
    */
   onPullDownRefresh: function () { 
    
   }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})